/*
 * Configuration.java
 *
 * Created on 2006 by moon 9:31
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.zll.framework.conf;

import java.io.BufferedInputStream;

import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.Properties;


public class Configuration
{
	/**** 싱글톤 ****/
	private final static Configuration conf = new Configuration();
	private Configuration() 
	{
		//get home path
		URL location = Configuration.class.getProtectionDomain().getCodeSource().getLocation();
		String dir = location.getFile();
		homePath = dir.substring(0, dir.indexOf("WEB-INF")-1);
		
		System.out.println("homePath:" + homePath);
		
		//read properties
		loadProps();
		
	}
	
	public static Configuration getInstance() {
		return conf;
	}
	
	
	/**** 로컬변수 ****/
	private Properties props = null;
	private String homePath = "";
	
	/**** 로컬함수 ****/
	private void loadProps() {
		try {
			//java.net.URL dbURL = ClassLoader.getSystemResource("default.properties");
			String filePath = this.homePath + File.separator+"WEB-INF" + File.separator + "default.properties";
			File configFile = new File(filePath);
			
			if ( ! configFile.canRead() ) {
	            System.out.println( "Can't open configuration file:" + filePath);
			}
	         
	         props = new java.util.Properties();
	         FileInputStream fin = new FileInputStream(configFile);
	         props.load(new BufferedInputStream(fin));
	         fin.close();
		} catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	
	
	/**** static 함수 ****/
	public static void reloadProps() {
		conf.loadProps();
	}
	
	public static String getHomePath() {
		return conf.homePath;
	}

    public static String getDataSourceName() {
    	return conf.props.getProperty("datasource");
    }

    public static String getDbName() {
    	return conf.props.getProperty("db.product.name");
    }

    public static String getWasName() {
    	return conf.props.getProperty("was.product.name");
    }

    public static String getPackageName() {
    	return conf.props.getProperty("package.name");
    }
    
    public static String getDomain() {
    	return conf.props.getProperty("domain");
    }
    
    public static String get(String key) {
    	return conf.props.getProperty(key);
    }
    
    public static boolean getBoolean(String key) {
    	boolean value = false;
        try {
           value = Boolean.valueOf(conf.props.getProperty(key)).booleanValue();
        } catch(Exception ex) {
           throw new IllegalArgumentException("Illegal boolean key : "+key);
        }
        return value;
    }
}
